package com.example.controller;

import com.example.bean.Person;
import com.example.dao.PersonRepository;
import com.example.services.ServicesTest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class DataController {

	private static Logger log = LoggerFactory.getLogger(DataController.class);

	@Autowired
	private PersonRepository personRepository;
	@Autowired
	private ServicesTest servicesTest;
	@Autowired
	private JdbcTemplate jdbcTemplate;

	@RequestMapping("/save")
	public Person save(String name,String address,Integer age){

		Person p = new Person(null,name,age,address);

		Person result = servicesTest.save(p);



		return result;
	}




	/**
	 * 测试findByAddress
	 */
	@RequestMapping("/q1")
	public List<Person> q1(String address){

		List<Person> people = personRepository.findByAddress(address);


		List<Map<String, Object>> mapList = jdbcTemplate.queryForList("select * from person");


		return people;

	}

	/**
	 * 测试findByNameAndAddress
	 */
	@RequestMapping("/q2")
	public Person q2(String name,String address){

		Person people = personRepository.findByNameAndAddress(name, address);

		return people;

	}

	/**
	 * 测试withNameAndAddressQuery
	 */
	@RequestMapping("/q3")
	public Person q3(String name,String address){

		Person p = personRepository.withNameAndAddressQuery(name, address);

		return p;

	}

	/**
	 * 测试withNameAndAddressNamedQuery
	 */
	@RequestMapping("/q4")
	public Person q4(String name,String address){

		Person p = personRepository.withNameAndAddressNamedQuery(name, address);

		return p;

	}

	/**
	 * 测试排序
	 */
	@RequestMapping("/sort")
	public List<Person> sort(){

		List<Person> people = personRepository.findAll(new Sort(Sort.Direction.DESC,"age"));

		return people;

	}

	/**
	 * 测试分页
	 */
	@RequestMapping("/page")
	public Page<Person> page(){

		Page<Person> pagePeople = personRepository.findAll(new PageRequest(1, 2));


		return pagePeople;

	}










}
